﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace DotNetNuke.Modules.ActiveForums.Controls
    <DefaultProperty("Text"), ToolboxData("<{0}:RequiredFieldValidator runat=server></{0}:RequiredFieldValidator>")> _
    Public Class RequiredFieldValidator
        Inherits WebControl
#Region "Delcarations"
        Private _Text As String
        Private _ControlToValidate As String
        Private _ValidationGroup As String
        Private _DefaultValue As String
#End Region
#Region "Properties"
        Public Property Text() As String
            Get
                Return _Text
            End Get
            Set(ByVal value As String)
                _Text = value
            End Set
        End Property
        Public Property ControlToValidate() As String
            Get
                Return _ControlToValidate
            End Get
            Set(ByVal value As String)
                _ControlToValidate = value
            End Set
        End Property
        Public Property ValidationGroup() As String
            Get
                Return _ValidationGroup
            End Get
            Set(ByVal value As String)
                _ValidationGroup = value
            End Set
        End Property
        Public Property DefaultValue() As String
            Get
                Return _DefaultValue
            End Get
            Set(ByVal value As String)
                _DefaultValue = value
            End Set
        End Property
#End Region
        Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
            If Enabled Then
                output.AddAttribute("class", CssClass)
                output.AddAttribute("id", ClientID)
                output.RenderBeginTag(HtmlTextWriterTag.Span)
                output.RenderEndTag()
                Dim sb As New StringBuilder
                sb.Append("<script>")
                sb.Append("if(!window.AMPage){window.AMPage=new AMValidator();};")
                Dim ctrl As New Control
                ctrl = Parent.FindControl(ControlToValidate)
                'Text = Text.Replace("'", "\'")
                sb.Append("AMPage.Add('" & ClientID & "','" & ctrl.ClientID & "','" & ValidationGroup & "',null,null,null,'" & Text & "','" & DefaultValue & "');")
                sb.Append("</script>")
                output.Write(sb.ToString)
            End If

        End Sub
        Private Sub RequiredFieldValidator_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Page.ClientScript.IsClientScriptIncludeRegistered("AMValidation") Then
                Page.ClientScript.RegisterClientScriptInclude("AMValidation", Page.ClientScript.GetWebResourceUrl(Me.GetType, "Validation.js"))
            End If
        End Sub
    End Class
End Namespace
